<?php

namespace app\savant\controller;

use think\Request;
use think\facade\Log;
use app\common\enums\ResultEnum;
use app\common\model\VideoDetail;
use app\common\utils\ResultVOUtil;
use app\common\exception\ZeduException;
use app\common\utils\OssFlies;

class Media extends Base
{
    /**
     * 查询
     *
     * @return \think\Response
     */
    public function index($id=0)
    {
        try {
            $model = VideoDetail::all(['vid'=>$id]);
            return ResultVOUtil::success($model);            
        } catch (\Exception $e) {
            Log::record('[视频文件]查询:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
        
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 上传视频
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function upload(Request $request,$id=0)
    {

        $validate   = new \app\common\validate\VideoDetail();
        $data = $request->param();
        $data['file'] = $request->file('file');
        if(!$validate->check($data)) {
            throw new ZeduException(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>$validate->getError()]);
        }
        
        if($data['file']){
            $oss = new OssFlies();
            $url = $oss->uploadOss($data['file'],'video');
            if($url){
                try {
                    $save['vid'] = $id;
                    $save['title'] = $data['title'];
                    $save['url'] = $url;
                    $model = new VideoDetail();
                    $model->allowField(true)->save($save);
                    
                    return ResultVOUtil::success($model);
                } catch (\Exception $e) {
                    Log::record('[视频文件]添加:'.$e->getMessage(),'error');
                    return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
                }
            }else{
                return ResultVOUtil::error(ResultEnum::UPLOAD_ERROR); 
            }
        }else{
            return ResultVOUtil::error(ResultEnum::UPLOAD_ERROR['code'],'上传为空');
        }
        
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $model = VideoDetail::destroy($id);
            return ResultVOUtil::success($model);
        } catch (\Exception $e) {
            Log::record('[视频文件]删除:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        }
    }
}
